﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ZLCMS.Business;
using NHibernate;
using ZLCMS.Data.Model;
using NHibernate.Criterion;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Util;
using Newtonsoft.Json;
using DotNet.Common.NHibernateUtil;
using NHibernate.Transform;

namespace ZLCMS.Web.SystemManage.Basics.Shop.MemberCard
{
    public partial class MemberCardLogList : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        public string _name;
        public string _starttime;
        public string _endtime;
        public MemberInfo _member;

        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化全局变量值
            _name = Request["name"] == null ? "" : Request["name"];
            _starttime = Request["starttime"] == null ? "" : Request["starttime"];
            _endtime = Request["endtime"] == null ? "" : Request["endtime"];
       

            //初始化DWZ分页控件
            _framePage = new FrameHelper(this) {  };

            //初始化控件值
            tb_search_name.Text = _name;
            tb_start.Text = _starttime;
            tb_end.Text = _endtime;

            if (!IsPostBack)
            {
                string opt = Request["opt"];
                //判断操作类型
                if (opt != null)
                {
                   // ShowAndDropCallBack(opt);
                }
            }

         
            //填充网格数据
            BindList(_framePage.PageIndex, _framePage.PageSize);

        }
        #region 填充网格数据 + protected void BindList(int pageIndex, int pageSize)
        /// <summary>
        /// 填充网格数据
        /// </summary>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        protected void BindList(int pageIndex, int pageSize)
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria=null;
                if (_name.Trim() != "")
                {
                    criteria = session.CreateCriteria<MemberInfo>().Add(Expression.Eq("MemberName", _name));

                  _member=   criteria.UniqueResult<MemberInfo>();
                }

                string selectStr = "select  c.Member_Account as MemberAccount,c.Id  as Id ,c.Amount as Amount ,c.Balance as Balance ,c.Opt_Time as OptTime ,c.Opt_Type as OptType ,m.Member_Name as MemberName ";

                string fromStr = " from Member_card_log c ,Member_Info m where c.Member_Account=m.Account_Id   ";
                IQuery query = session.CreateSQLQuery(selectStr + fromStr);

                if(_member!=null)
                {
                    selectStr += " and c.Member_Account=:acc ";
                }
                SetupCondition(selectStr);

                if (_member != null)
                {
                    query.SetParameter("acc",_member.AccountId);
                }
                if (_starttime != "")
                {
                    query.SetParameter("start", _member.AccountId);
                }

                if (_endtime != "")
                {
                    query.SetParameter("end", _member.AccountId);
                }

                query.SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize);

                grid.DataSource = query.SetResultTransformer(Transformers.AliasToEntityMap).List(); ;
                grid.DataBind();

                _framePage.RecordCount =Convert.ToInt32( session.CreateSQLQuery(" select count(*) " + fromStr).UniqueResult());
            }
        }
        #endregion

        #region 填充查询条件 + private void SetupCondition(ref ICriteria criteria)
        /// <summary>
        /// 填充查询条件
        /// </summary>
        /// <param name="criteria"></param>
        private void SetupCondition( string sql)
        {
            if (_starttime != "")
            {
                sql += " and c.Opt_Time>:start ";
              
            }

            if (_endtime != "")
            {
                sql += " and c.Opt_Time<:end ";
          
            }
            sql += " order by c.Opt_Time desc";
        }
        #endregion

    }
}
